package com.aerozhonghuan.logisticsmall.modules.order;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.base.TitlebarFragment;
import com.aerozhonghuan.logisticsmall.modules.common.entity.GoodsObject;
import com.aerozhonghuan.logisticsmall.modules.common.entity.ProductImg;
import com.aerozhonghuan.logisticsmall.modules.order.beans.SubmitOrderInfo;
import com.aerozhonghuan.logisticsmall.modules.order.logic.OrderWebRequest;
import com.aerozhonghuan.logisticsmall.utils.CommunityTool;
import com.aerozhonghuan.logisticsmall.widget.ProgressDialogIndicator;
import com.aerozhonghuan.oknet2.CommonCallback;
import com.aerozhonghuan.oknet2.CommonMessage;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述 订单确认页面
 * 作者 Zhang Yonghui
 * 创建日期 2019-05-17
 */
public class OrderConfirmFragment extends TitlebarFragment implements View.OnClickListener {

    private View rootView;
    private Button mBtn;
    private TextView mTvTotalMoney;
    private List<GoodsObject> mGoodsObjectList = new ArrayList<>();
    private String mTotalPrice;
    private ImageView mIvPoint;
    private TextView mTvStoreName;
    private String mStoreInfo_name;
    private int billId;
    private int mStoreInfo_id;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_order_confirm, null);
            if (getArguments() == null || !getArguments().containsKey("list")) {
                alert("数据异常");
                getActivity().finish();
            }
            mTotalPrice = getArguments().getString("totalPrice");
            billId = getArguments().getInt("billId");
            mGoodsObjectList = (ArrayList<GoodsObject>) getArguments().get("list");
            LogUtils.i(mTotalPrice);
            initView();
            initData();
        }
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (StringUtils.isEmpty(SPUtils.getInstance().getString("StoreInfo_name"))) {
            mBtn.setEnabled(false);
            mIvPoint.setImageResource(R.drawable.net_point_none);
            mTvStoreName.setText("请选择服务网点");
        } else {
            mBtn.setOnClickListener(this);
            mBtn.setEnabled(true);
            mIvPoint.setImageResource(R.drawable.net_point);
            getStoreInfo();
            mTvStoreName.setText(mStoreInfo_name);
        }
    }

    private void getStoreInfo() {
        int storeInfo_distance = SPUtils.getInstance().getInt("StoreInfo_distance");
        mStoreInfo_id = SPUtils.getInstance().getInt("StoreInfo_id");
        String storeInfo_address = SPUtils.getInstance().getString("StoreInfo_address");
        mStoreInfo_name = SPUtils.getInstance().getString("StoreInfo_name");
        String StoreInfo_contactNumber = SPUtils.getInstance().getString("StoreInfo_contactNumber");
    }

    private void initView() {
        mTvStoreName = (TextView) rootView.findViewById(R.id.tv_store_name);
        View selectLocation = rootView.findViewById(R.id.view_select_loc);
        mIvPoint = (ImageView) rootView.findViewById(R.id.iv_point);
        selectLocation.setOnClickListener(this);
        mBtn = (Button) rootView.findViewById(R.id.btn_confirm);
        mBtn.setEnabled(false);
        mTvTotalMoney = (TextView) rootView.findViewById(R.id.tv_total_money);

        mTvTotalMoney.setText("¥ " + mTotalPrice);

        RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        MyRvAdapter adapter = new MyRvAdapter();
        rv.setAdapter(adapter);
    }

    private void initData() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_select_loc:
                ActivityUtils.startActivity(SelectStoreActivity.class);
                break;
            case R.id.btn_confirm:
                OrderWebRequest.submitOrder(billId + "", mStoreInfo_id, getActivity(), new ProgressDialogIndicator(getContext()), new CommonCallback<SubmitOrderInfo>() {
                    @Override
                    public void onSuccess(SubmitOrderInfo messsageBodyObject, CommonMessage commonMessage, String allResponseString) {
                        ActivityUtils.startActivity(new Intent(OrderConfirmFragment.this.getContext(), SubmitResultActivity.class)
                                .putExtra("billId", String.valueOf(billId))
                                .putExtra("code", messsageBodyObject.billNumber)
                                .putExtra("money", mTotalPrice));
                        if (getActivity() != null) {
                            getActivity().finish();
                        }
                    }
                });
                break;
            default:
                break;
        }
    }

    class MyRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyHolder(View.inflate(OrderConfirmFragment.this.getContext(), R.layout.item_for_order_confirm_fragment, null));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((MyHolder) holder).setData(mGoodsObjectList.get(position), position);
        }

        @Override
        public int getItemCount() {
            return mGoodsObjectList == null ? 0 : mGoodsObjectList.size();
        }
    }

    class MyHolder extends RecyclerView.ViewHolder {
        private RequestOptions options;
        private View mLine;
        private TextView mTvName;
        private TextView mTvPrice;
        private TextView mTvQuantity;
        private ImageView mIvProduct;
        private ConstraintLayout mClContent;

        MyHolder(View itemView) {
            super(itemView);
            mLine = itemView.findViewById(R.id.line);
            mTvName = (TextView) itemView.findViewById(R.id.tv_name);
            mTvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            mTvQuantity = (TextView) itemView.findViewById(R.id.tv_quantity);
            mIvProduct = (ImageView) itemView.findViewById(R.id.iv_product);
            mClContent = (ConstraintLayout) itemView.findViewById(R.id.cl_content);
            options = new RequestOptions().placeholder(R.mipmap.ic_placehodler).error(R.mipmap.ic_placehodler);
        }

        @SuppressLint("SetTextI18n")
        public void setData(GoodsObject goodsObject, int position) {
            if (mGoodsObjectList.size() >= 1 && mGoodsObjectList.size() - 1 == position) {
                mLine.setVisibility(View.INVISIBLE);
            } else {
                mLine.setVisibility(View.VISIBLE);
            }
            mTvName.setText(goodsObject.getBaseName());
            mTvPrice.setText(goodsObject.getPrice() / 100f + "");
            mTvQuantity.setText("X" + goodsObject.getIntoGouwucheSize());
            List<ProductImg> productImgs = goodsObject.getpImageList();
            int productImgId = 0;
            if (ObjectUtils.isNotEmpty(productImgs)) {
                for (ProductImg productImg : productImgs) {
                    if (productImg.is_thumbnail == 1) {
                        productImgId = productImg.imgid;
                        break;
                    } else {
                        productImgId = productImg.imgid;
                    }
                }
            }
            Glide.with(OrderConfirmFragment.this).load(CommunityTool.changeImageId(productImgId, true, false))
                    .apply(options)
                    .into(mIvProduct);

            if (mGoodsObjectList.size() == 1) {
                mClContent.setBackground(OrderConfirmFragment.this.getResources().getDrawable(R.drawable.bg_alert));
                mLine.setVisibility(View.INVISIBLE);
                return;
            }
            if (position == 0) {
                mClContent.setBackground(OrderConfirmFragment.this.getResources().getDrawable(R.drawable.bg_white_top));
            } else if (mGoodsObjectList.size() - 1 == position) {
                mClContent.setBackground(OrderConfirmFragment.this.getResources().getDrawable(R.drawable.bg_white_bottom));
            } else {
                mClContent.setBackgroundColor(ColorUtils.getColor(R.color.white));
            }
        }
    }
}
